﻿Public Class fPrehledyDetailZobrazeni
    Public datumOd As Date
    Public datumDo As Date
    '---
    'prepare vars of lowest and highest date of selected time-span
    Dim indexL As Long = Nothing
    Dim indexU As Long = Nothing

    


    Private Sub fPrehledyDetailZobrazeni_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub


    Private Sub fPrehledyDetailZobrazeni_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'loads necessary data from dtb/dataset
        Me.Seznam_PrijemPostovneTableAdapter1.Fill(Me.DtbDataSet1.seznam_PrijemPostovne)
        Me.Seznam_vstupyTableAdapter1.Fill(Me.DtbDataSet1.seznam_vstupy)
        Me.Seznam_vyrobkyTableAdapter1.Fill(Me.DtbDataSet1.seznam_vyrobky)
        Me.Tab_prijemTableAdapter1.Fill(Me.DtbDataSet1.tab_prijem) 'fills table ordered by "datum_pořízení" ASC
        Me.Tab_vydajTableAdapter1.Fill(Me.DtbDataSet1.tab_vydaj) ''fills table ordered by "datum_pořízení" ASC
        'sets properties of some controls
        'Me.DGV_Prehledy.Visible = False
        Me.dpickOd.Value = Now.Date
        Me.dpickDo.Value = Now.Date


    End Sub


    Private Sub chboxPrijmy_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chboxPrijmy.CheckedChanged
        If chboxPrijmy.CheckState = CheckState.Checked Then
            Me.chboxVydaje.Enabled = False
        ElseIf chboxPrijmy.CheckState = CheckState.Unchecked Then
            Me.chboxVydaje.Enabled = True
        End If
    End Sub

    Private Sub chboxVydaje_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chboxVydaje.CheckedChanged
        If chboxVydaje.CheckState = CheckState.Checked Then
            Me.chboxPrijmy.Enabled = False
        ElseIf Me.chboxVydaje.CheckState = CheckState.Unchecked Then
            Me.chboxPrijmy.Enabled = True
        End If
    End Sub

    Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
        Me.Close()
    End Sub

    Private Sub btnGenerovat_Click(sender As System.Object, e As System.EventArgs) Handles btnGenerovat.Click
        'get tables
        Dim tabPrijmy As DataTable = Me.DtbDataSet1.tab_prijem
        Dim tabVydaje As DataTable = Me.DtbDataSet1.tab_vydaj
        Dim tabPostovne As DataTable = Me.DtbDataSet1.seznam_PrijemPostovne
        Dim tabVyrobky As DataTable = Me.DtbDataSet1.seznam_vyrobky
        Dim tabVstupy As DataTable = Me.DtbDataSet1.seznam_vstupy


        Dim radkyPrijmyCntr As Long = tabPrijmy.Rows.Count()
        Dim radkyVydajeCntr As Long = tabVydaje.Rows.Count()
        Dim arrPrijDatPor(radkyPrijmyCntr - 1) As Date
        Dim arrVydeDatPor(radkyVydajeCntr - 1) As Date

        indexL = Nothing
        indexU = Nothing

        DGV_Prehledy.Rows.Clear()
        DGV_Prehledy.Columns.Clear()
        
        '---
        For i As Long = 0 To radkyPrijmyCntr - 1
            arrPrijDatPor(i) = tabPrijmy.Rows.Item(i).Item(8)
        Next i
        For i As Long = 0 To radkyVydajeCntr - 1
            arrVydeDatPor(i) = tabVydaje.Rows.Item(i).Item(7)
        Next i
        '----sets time-span range for report-----
        '----
        If Me.chboxCeleObdobi.CheckState = CheckState.Checked Then 'sets report's time-span, if all records are to be displayed

            If Me.chboxPrijmy.CheckState = CheckState.Checked Then 'if income should be reported
                'Dim numRows As Long = tabPrijmy.Rows.Count()
                Dim arrUbound As Long = arrPrijDatPor.GetUpperBound(0)
                Me.datumOd = arrPrijDatPor(0)
                indexL = 0
                Me.datumDo = arrPrijDatPor(arrUbound)
                indexU = arrUbound
            ElseIf Me.chboxVydaje.CheckState = CheckState.Checked Then 'if expenses should be reported
                'Dim numRows As Long = tabVydaje.Rows.Count()
                Dim arrUbound As Long = arrVydeDatPor.GetUpperBound(0)
                Me.datumOd = arrVydeDatPor(0)
                indexL = 0
                Me.datumDo = arrVydeDatPor(arrUbound)
                indexU = arrUbound
            Else
                MsgBox("Nelze určit rozsah výběru pro zobrazení!", MsgBoxStyle.Critical, "Chyba!") 'if can't decide
                'Me.btnOK.PerformClick()
                Exit Sub
            End If

        ElseIf Me.chboxObdobiAktualniRok.CheckState = CheckState.Checked Then 'sets report's time span, if actual year is selected

            If Me.chboxPrijmy.CheckState = CheckState.Checked Then 'if income is to be reported
                'Dim numRows As Long = tabPrijmy.Rows.Count() 'get first date which >= the beginning of the current year
                Dim arrUbound As Long = arrPrijDatPor.GetUpperBound(0)
                For i As Long = 0 To arrUbound
                    Dim datum As Date = arrPrijDatPor(i)
                    If Date.Compare(datum, Me.dpickOd.Value) >= 0 Then
                        Me.datumOd = datum
                        indexL = i
                        indexU = arrUbound
                        Exit For
                    End If
                Next i
                Me.datumDo = arrPrijDatPor(arrUbound)
            ElseIf Me.chboxVydaje.CheckState = CheckState.Checked Then 'if expenses are to be reported
                'Dim numRows As Long = tabVydaje.Rows.Count()
                Dim arrUbound As Long = arrVydeDatPor.GetUpperBound(0)
                For i As Long = 0 To arrUbound
                    Dim datum As Date = arrVydeDatPor(i)
                    If Date.Compare(datum, Me.dpickOd.Value) >= 0 Then
                        Me.datumOd = datum
                        indexL = i
                        indexU = arrUbound
                        Exit For
                    End If
                Next i
                Me.datumDo = arrVydeDatPor(arrUbound)
            Else
                MsgBox("Nelze určit rozsah výběru pro zobrazení!", MsgBoxStyle.Critical, "Chyba!")
                'Me.btnOK.PerformClick()
                Exit Sub
            End If

        Else 'sets report's time-span, if values are picked from pickers, no chbox is selected

            Dim _od As Date = Me.dpickOd.Value
            Dim _do As Date = Me.dpickDo.Value

            If Me.chboxPrijmy.CheckState = CheckState.Checked Then 'income chbox is selected
                'Dim numRows As Long = tabPrijmy.Rows.Count()
                Dim arrUbound As Long = arrPrijDatPor.GetUpperBound(0)
                For i As Long = 0 To arrUbound
                    Dim datum As Date = arrPrijDatPor(i)
                    If Date.Compare(datum, _od) >= 0 Then
                        Me.datumOd = datum
                        indexL = i
                        Exit For
                    End If
                Next i
                For i As Long = 0 To arrUbound
                    Dim datum As Date = arrPrijDatPor(i)
                    If Date.Compare(datum, _do) > 0 Then
                        Me.datumDo = arrPrijDatPor(i - 1)
                        indexU = i - 1
                        Exit For
                    ElseIf i = arrUbound Then
                        Me.datumDo = arrPrijDatPor(arrUbound)
                        indexU = i
                    End If
                Next i

            ElseIf Me.chboxVydaje.CheckState = CheckState.Checked Then 'expenses chbox is selected
                'Dim numRows As Long = tabVydaje.Rows.Count()
                Dim arrUbound As Long = arrVydeDatPor.GetUpperBound(0)
                For i As Long = 0 To arrUbound
                    Dim datum As Date = arrVydeDatPor(i)
                    If Date.Compare(datum, _od) >= 0 Then
                        Me.datumOd = datum
                        indexL = i
                        Exit For
                    End If
                Next i
                For i As Long = indexL To arrUbound
                    Dim datum As Date = arrVydeDatPor(i)
                    If Date.Compare(datum, _do) > 0 Then
                        Me.datumDo = arrVydeDatPor(i - 1)
                        indexU = i - 1
                        Exit For
                    ElseIf i = arrUbound Then
                        Me.datumDo = arrVydeDatPor(arrUbound)
                        indexU = i
                    End If
                Next i

            Else
                MsgBox("Nelze určit rozsah výběru pro zobrazení!", MsgBoxStyle.Critical, "Chyba!")
                'Me.btnOK.PerformClick()
                Exit Sub
            End If

        End If
        '---
        '----end of time-span setting----
        '---
        '----preparing data to display
        Dim numRow As Long = Nothing
        Dim colDatPor As New DataColumn With {.ColumnName = "colDatPor", .DataType = GetType(Date)}
        Dim colDatSpl As New DataColumn With {.ColumnName = "colDatSpl", .DataType = GetType(Date)}
        Dim colCastka As New DataColumn With {.ColumnName = "colCastka", .DataType = GetType(Decimal)}
        Dim colNazev As New DataColumn With {.ColumnName = "colNazev", .DataType = GetType(String)}
        Dim colPost As New DataColumn With {.ColumnName = "colPost", .DataType = GetType(Decimal)}
        Dim colZisk As New DataColumn With {.ColumnName = "colZisk", .DataType = GetType(Decimal)}
        Dim colCastkaKumul As New DataColumn With {.ColumnName = "colCastkaKumul", .DataType = GetType(Decimal)}
        Dim colZiskKumul As New DataColumn With {.ColumnName = "colZiskKumul", .DataType = GetType(Decimal)}

        With DGV_Prehledy
            .Columns.Add(colDatPor.ColumnName, "Datum pořízení/prodeje")
            .Columns.Add(colDatSpl.ColumnName, "Datum splatnosti")
            .Columns.Add(colNazev.ColumnName, "Název položky")
            .Columns.Add(colCastka.ColumnName, "Celková hodnota položky")
            .Columns.Add(colCastkaKumul.ColumnName, "Kumulovaný součet")
            .Columns.Add(colPost.ColumnName, "Poštovné")
        End With
        'if income list is to be displayed:
        If Me.chboxPrijmy.CheckState = CheckState.Checked Then 'get num of records in selected table
            numRow = tabPrijmy.Rows.Count()
            If numRow = 0 Then 'if selected table is empty
                MsgBox("Zvolená tabulka neobsahuje žádný záznam!", MsgBoxStyle.Information, "Upozornění!")
                Exit Sub
            Else
                Try
                    Dim dateRange As Long = indexU - indexL
                    Dim arrDGVrow(dateRange, 5) As Object
                    'fill array with rows to be displayed
                    For i As Long = 0 To dateRange
                        arrDGVrow(i, 0) = tabPrijmy.Rows.Item(i + indexL).Item(8)
                        arrDGVrow(i, 1) = tabPrijmy.Rows.Item(i + indexL).Item(9)

                        For j As Long = 0 To tabVyrobky.Rows.Count() - 1
                            If tabVyrobky.Rows.Item(j).Item(0) = _
                                tabPrijmy.Rows.Item(i + indexL).Item(3) Then
                                arrDGVrow(i, 2) = tabVyrobky.Rows.Item(j).Item(1)
                                Exit For
                            End If
                        Next j

                        arrDGVrow(i, 3) = tabPrijmy.Rows.Item(i + indexL).Item(10) * _
                            tabPrijmy.Rows.Item(i + indexL).Item(11)

                        If i = 0 Then
                            arrDGVrow(i, 4) = arrDGVrow(i, 3)
                        Else
                            arrDGVrow(i, 4) = arrDGVrow(i - 1, 4) + arrDGVrow(i, 3)
                        End If

                        For j As Long = 0 To tabPostovne.Rows.Count() - 1
                            If tabPostovne.Rows.Item(j).Item(0) = _
                                tabPrijmy.Rows.Item(i + indexL).Item(7) Then
                                arrDGVrow(i, 5) = tabPostovne.Rows.Item(j).Item(1)
                                Exit For
                            End If
                        Next j
                    Next i
                    'create DGV rows and populate cells; display result
                    For i As Long = 0 To arrDGVrow.GetUpperBound(0)
                        Dim DGVrow As New DataGridViewRow() With {.ReadOnly = True}
                        Dim arrTemp(5) As Object

                        For j As Long = 0 To arrTemp.GetUpperBound(0)
                            arrTemp(j) = arrDGVrow(i, j)
                        Next j

                        DGVrow.CreateCells(DGV_Prehledy, arrTemp)
                        DGV_Prehledy.Rows.Add(DGVrow)

                    Next i

                    DGV_Prehledy.Update()

                Catch ex As Exception
                    MsgBox("Chyba při zpracování přehledu příjmů." & vbCrLf _
                           & ex.Message, MsgBoxStyle.Critical, "Chyba!")
                    Exit Sub
                End Try
            End If
            'if expenses list is to be displayed:
        ElseIf Me.chboxVydaje.CheckState = CheckState.Checked Then
            numRow = tabVydaje.Rows.Count()
            If numRow = 0 Then 'if selected table is empty
                MsgBox("Zvolená tabulka neobsahuje žádný záznam!", MsgBoxStyle.Information, "Upozornění!")
                Exit Sub
            Else
                Try
                    Dim dateRange As Long = indexU - indexL
                    Dim arrDGVrow(dateRange, 5) As Object
                    'fill array with rows to be displayed
                    For i As Long = 0 To dateRange
                        arrDGVrow(i, 0) = tabVydaje.Rows.Item(i + indexL).Item(7)
                        arrDGVrow(i, 1) = tabVydaje.Rows.Item(i + indexL).Item(8)

                        For j As Long = 0 To tabVstupy.Rows.Count() - 1
                            If tabVstupy.Rows.Item(j).Item(0) = _
                                tabVydaje.Rows.Item(i + indexL).Item(4) Then
                                arrDGVrow(i, 2) = tabVstupy.Rows.Item(j).Item(1)
                                Exit For
                            End If
                        Next j

                        arrDGVrow(i, 3) = tabVydaje.Rows.Item(i + indexL).Item(9) * _
                            tabVydaje.Rows.Item(i + indexL).Item(10)

                        If i = 0 Then
                            arrDGVrow(i, 4) = arrDGVrow(i, 3)
                        Else
                            arrDGVrow(i, 4) = arrDGVrow(i - 1, 4) + arrDGVrow(i, 3)
                        End If

                        For j As Long = 0 To tabPostovne.Rows.Count() - 1
                            If tabPostovne.Rows.Item(j).Item(0) = _
                                tabVydaje.Rows.Item(i + indexL).Item(5) Then
                                arrDGVrow(i, 5) = tabPostovne.Rows.Item(j).Item(1)
                                Exit For
                            End If
                        Next j
                    Next i
                    'create DGV row and populate cells; display result

                    For i As Long = 0 To arrDGVrow.GetUpperBound(0)
                        Dim DGVrow As New DataGridViewRow() With {.ReadOnly = True}
                        Dim arrTemp(5) As Object

                        For j As Long = 0 To arrTemp.GetUpperBound(0)
                            arrTemp(j) = arrDGVrow(i, j)
                        Next j

                        DGVrow.CreateCells(DGV_Prehledy, arrTemp)
                        DGV_Prehledy.Rows.Add(DGVrow)

                    Next i

                    DGV_Prehledy.Update()

                Catch ex As Exception
                    MsgBox("Chyba při zpracování přehledu výdajů." & vbCrLf _
                           & ex.Message, MsgBoxStyle.Critical, "Chyba!")
                    Exit Sub
                End Try
            End If
        Else
            MsgBox("Pro správné zobrazení je nutno zvolit buď položku Příjem či Výdaj", MsgBoxStyle.Critical, "Chyba!")
            'Me.btnOK.PerformClick()
            Exit Sub
        End If

        Me.btnGenerovat.Enabled = False

    End Sub

    Private Sub chboxCeleObdobi_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chboxCeleObdobi.CheckedChanged
        If Me.chboxCeleObdobi.CheckState = CheckState.Checked Then
            Me.dpickOd.Enabled = False
            Me.dpickDo.Enabled = False
            Me.chboxObdobiAktualniRok.Enabled = False
        ElseIf Me.chboxCeleObdobi.CheckState = CheckState.Unchecked Then
            Me.dpickOd.Enabled = True
            Me.dpickDo.Enabled = True
            Me.chboxObdobiAktualniRok.Enabled = True
        End If
    End Sub

    Private Sub chboxObdobiAktualniRok_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chboxObdobiAktualniRok.CheckedChanged
        If Me.chboxObdobiAktualniRok.CheckState = CheckState.Checked Then
            Me.chboxCeleObdobi.Enabled = False
            Me.dpickOd.Enabled = False
            Me.dpickDo.Enabled = False

            Dim now As Date = Date.Now.Date
            Me.dpickDo.Value = now
            Me.dpickOd.Value = "01.01." & now.Year.ToString & " 00:00:00"
        ElseIf Me.chboxObdobiAktualniRok.CheckState = CheckState.Unchecked Then
            Me.chboxCeleObdobi.Enabled = True
            Me.dpickOd.Enabled = True
            Me.dpickDo.Enabled = True

            Me.dpickOd.Value = Now
            Me.dpickDo.Value = Now
        End If
    End Sub

    Private Sub btnVymazat_Click(sender As System.Object, e As System.EventArgs) Handles btnVymazat.Click
        indexL = Nothing
        indexU = Nothing
        '--
        DGV_Prehledy.Columns.Clear()
        DGV_Prehledy.Rows.Clear()
        '--
        If Me.chboxPrijmy.CheckState = CheckState.Checked Then
            Me.chboxPrijmy.CheckState = CheckState.Unchecked
        End If
        If Me.chboxVydaje.CheckState = CheckState.Checked Then
            Me.chboxVydaje.CheckState = CheckState.Unchecked
        End If
        If Me.chboxCeleObdobi.CheckState = CheckState.Checked Then
            Me.chboxCeleObdobi.CheckState = CheckState.Unchecked
        End If
        If Me.chboxObdobiAktualniRok.CheckState = CheckState.Checked Then
            Me.chboxObdobiAktualniRok.CheckState = CheckState.Unchecked
        End If

        Me.btnGenerovat.Enabled = True

        Me.dpickOd.Value = Now.Date
        Me.dpickDo.Value = Now.Date


    End Sub

    Private Sub dpickOd_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dpickOd.ValueChanged
        Me.dpickOd.Value = dpickOd.Value.Date
    End Sub

    Private Sub dpickDo_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dpickDo.ValueChanged
        Me.dpickDo.Value = dpickDo.Value.Date
    End Sub

    Private Sub btnGraf_Click(sender As System.Object, e As System.EventArgs) Handles btnGraf.Click
        My.Forms.fPrehledyDetailGraf.ShowDialog()
    End Sub
End Class